package com.flowabledemo.service.impl;

import com.flowabledemo.dto.ProcessParam;
import com.flowabledemo.dto.TaskParam;
import com.flowabledemo.service.SimpleFlowService;
import org.flowable.bpmn.model.*;
import org.flowable.bpmn.model.Process;
import org.flowable.engine.repository.Model;

import java.util.List;

/**
 * @author :Administrator
 * @description: TODO edit class Description
 * @create date: 2022/2/18 16:48
 */
public class SimpleFlowServiceImpl implements SimpleFlowService {

    @Override
    public void saveModelData(ProcessParam processParam){
/*

        Model model = modelService.getModel(processParam.getModelId());

        Process process = new Process();
        process.setId(model.getKey());
        process.setName(model.getName());

        BpmnModel bpmnModelNew = new BpmnModel();
        bpmnModelNew.addProcess(process);

        List<TaskParam> nodeList = processParam.getTasks();

        //开始事件
        StartEvent startEvent = new StartEvent();
        startEvent.setId("start");
        startEvent.setName(processParam.getStartNodeName());

        process.addFlowElement(startEvent);
        FlowElement waitNode = startEvent;
        int i = 0;
        for ( ;i < nodeList.size(); i++) {
            TaskParam taskParam = nodeList.get(i);
            UserTask userTask = new UserTask();
            userTask.setId((i+1)+"");
            userTask.setName(taskParam.getName());
            if("user".equals(taskParam.getUserType())){
                userTask.setCandidateUsers(taskParam.getUserOrRoleIds());
            }
            if("role".equals(taskParam.getUserType())){
                userTask.setCandidateGroups(taskParam.getUserOrRoleIds());
            }
            process.addFlowElement(userTask);
            SequenceFlow sequenceFlow = new SequenceFlow(waitNode.getId(), userTask.getId());
            process.addFlowElement(sequenceFlow);
            //设置当前节点的出口这个很关键，不设置这个的话生的流程图会错乱，网上很多教程也没有提到
            if(i==0){
                ((StartEvent) waitNode).setOutgoingFlows(ListUtil.toList(sequenceFlow));
            }else {
                ((UserTask) waitNode).setOutgoingFlows(ListUtil.toList(sequenceFlow));
            }

            waitNode = userTask;
        }

        //结束事件
        EndEvent completeEvent = new EndEvent();
        completeEvent.setId("complete");
        completeEvent.setName(processParam.getEndNodeName());
        process.addFlowElement(completeEvent);

        SequenceFlow endSequenceFlow = new SequenceFlow(waitNode.getId(), completeEvent.getId());
        ((UserTask) waitNode).setOutgoingFlows(ListUtil.toList(endSequenceFlow));
        process.addFlowElement(endSequenceFlow);
        //这句很关键让程序自动部局各节点的位置，网上很多教程没有提到
        new BpmnAutoLayout(bpmnModelNew).execute();

        //把BpmnModel转成json格式进行保存
        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
        ObjectNode jsonNode = jsonConverter.convertToJson(bpmnModelNew);
        modelService.saveModel(model.getId(),model.getName(),model.getKey(),model.getDescription(),jsonNode.toString(),true,"",new UserWf());
*/

    }
}
